package testng.service;

import com.iwhalecloud.citybrain.flow.platform.manager.FlowEngineApplication;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowProcessService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.FlowUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ProcessHandleVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ProcessQueryVo;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.Assert;

/**
 * @author cheng.huaxing
 * @date 2019-03-13
 */
@SpringBootTest(classes = FlowEngineApplication.class)
public class FlowProcessServiceImplTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private FlowProcessService flowProcessService;

    //@Test
    public void testList() {
        ProcessQueryVo processQueryVo = new ProcessQueryVo();
        processQueryVo.setAppId(10);
        processQueryVo.setProcessStatus("executing");
        processQueryVo.setApplyUserName("成华星");
        ResponseResult responseResult = flowProcessService.list(processQueryVo);
        Assert.assertEquals(responseResult.getErrCode(), 0);
    }

    //@Test
    public void testList2() {
        ProcessQueryVo processQueryVo = new ProcessQueryVo();
        processQueryVo.setAppId(10);
        processQueryVo.setProcessStatus("abnormal");
        processQueryVo.setApplyUserName("成华星");
        ResponseResult responseResult = flowProcessService.list(processQueryVo);
        Assert.assertEquals(responseResult.getErrCode(), 0);
    }

    //@Test
    public void testList3() {
        ProcessQueryVo processQueryVo = new ProcessQueryVo();
        processQueryVo.setAppId(10);
        processQueryVo.setProcessStatus("completed");
        processQueryVo.setApplyUserName("成华星");
        ResponseResult responseResult = flowProcessService.list(processQueryVo);
        Assert.assertEquals(responseResult.getErrCode(), 0);
    }


    //@Test
    public void testHandleProcessInstance() {
        ProcessHandleVo processHandleVo = new ProcessHandleVo();
        processHandleVo.setProcessInstanceId("447501");
        processHandleVo.setProcessStatus("2");
        processHandleVo.setComment("挂起");
        ResponseResult responseResult = flowProcessService.handleProcessInstance(processHandleVo);
        Assert.assertEquals(responseResult.getErrCode(), 0);
    }

    //@Test
    public void testGetProcessInstanceDiagramAndInfo() {
        ResponseResult responseResult = flowProcessService.getProcessInstanceDiagramAndInfo("447501");
        Assert.assertEquals(responseResult.getErrCode(), 0);
    }

    //@Test
    public void testGetProcessInstanceCurrentTasks() {
        ProcessQueryVo queryVo = new ProcessQueryVo();
        queryVo.setProcessInstanceId("830042");
        queryVo.setPageNum(1);
        queryVo.setPageSize(10);
        ResponseResult responseResult = flowProcessService.getProcessInstanceCurrentTasks(queryVo);
        Assert.assertEquals(responseResult.getErrCode(), 0);
    }

    //@Test
    public void testGetProcessInstanceHandleLogs() {
        ProcessQueryVo processQueryVo = new ProcessQueryVo();
        processQueryVo.setProcessInstanceId("447501");
        ResponseResult responseResult = flowProcessService.getProcessInstanceHandleLogs(processQueryVo);
        Assert.assertEquals(responseResult.getErrCode(), 0);
    }

    //@Test
    public void testGetProcessInstanceMutiTaskInfo() {
        ResponseResult responseResult = flowProcessService.getProcessInstanceMutiTaskInfo("450007", "447501");
        Assert.assertEquals(responseResult.getErrCode(), 0);
    }

    //@Test
    public void testUtil() {
        FlowUtils.getInstance().findCurrentActivityId("447501");
//        FlowUtils.getInstance().findNewActivityId("447501", "ITS_APIFB");
        FlowUtils.getInstance().setCurrentTaskDueDate(FlowUtils.getInstance().getProcessInstance("872838"));
        TaskEntity taskEntity = new TaskEntityImpl();
        taskEntity.setProcessInstanceId("867508");
        taskEntity.setTaskDefinitionKey("kl_003");
        FlowUtils.getInstance().setTaskDueDate(taskEntity);
        FlowUtils.getInstance().setProcessInstanceParticipantsFromVariable(taskEntity);
        StringUtils.substringBetween("ITS_APIFB", "1", "3");
    }

    //@Test
    public void testGetFlowElementBeforeCurrentElement() {
        /*RepositoryService repositoryService = SpringUtils.getBean(RepositoryService.class);
        Process process = repositoryService.getBpmnModel("EDASITS20181102103144:1:445014").getMainProcess();
        List<FlowElement> flowElements = FlowUtils.getInstance().getFlowElementBeforeCurrentElement(process, "ITS_APIXX");
        for(FlowElement flowElement : flowElements) {
            System.out.println(flowElement.getId() + " ---> " + flowElement.getName());
        }*/
    }
}